package com.gaswell.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaswell.common.log.LogAnnotation;
import com.gaswell.pojo.ALLQj;
import com.gaswell.pojo.TimeTable;
import com.gaswell.service.ALLQjService;
import com.gaswell.service.TimeTableService;
import com.gaswell.utils.DateUtils;
import com.gaswell.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName timetable
 * @Author xiaow
 * @DATE 2023/2/21 15:32
 **/


@RestController
@RequestMapping("/timetable")
@Api(tags = "timetable 前端控制器   mysql数据库")
public class TimeTableController {

    @Autowired
    TimeTableService timeTableService;
    @Autowired
    ALLQjService allQjService;

    @GetMapping("getAll")
    public Result getAll(String userName, String jh) {
        LambdaQueryWrapper<ALLQj> allQjLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allQjLambdaQueryWrapper.eq(ALLQj::getYwbh, jh).or().eq(ALLQj::getZwbh, jh);
        ALLQj one = allQjService.getOne(allQjLambdaQueryWrapper);
        if (one != null) {
            LambdaQueryWrapper<TimeTable> timeTableLambdaQueryWrapper = new LambdaQueryWrapper<>();
            timeTableLambdaQueryWrapper.eq(TimeTable::getJh, one.getZwbh()).or().eq(TimeTable::getJh, one.getYwbh());
            List<TimeTable> list = timeTableService.list(timeTableLambdaQueryWrapper);
            HashMap<String, List<TimeTable>> stringlistHashMap = new HashMap<>();
            stringlistHashMap.put("0", new LinkedList<TimeTable>());
            stringlistHashMap.put("1", new LinkedList<TimeTable>());
            stringlistHashMap.put("2", new LinkedList<TimeTable>());
            list.forEach(l -> {
                if (l.getType() == 0) {
                    stringlistHashMap.get("0").add(l);
                } else if (l.getType() == 1)
                    stringlistHashMap.get("1").add(l);
                else
                    stringlistHashMap.get("2").add(l);
            });
            return Result.success(stringlistHashMap);
        } else {
            return Result.fail(400, "井号错误");
        }


    }

    @PostMapping("insertorupdate")
    @ApiOperation("添加或更新时刻表")
    @LogAnnotation(module = "RTU实时生产数据", operator = "按条件查询(多选)")
    public Result insertorupdate(String userName, @RequestBody TimetableVo timetableVo, String jh) {
        LambdaQueryWrapper<ALLQj> allQjLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allQjLambdaQueryWrapper.eq(ALLQj::getYwbh, jh).or().eq(ALLQj::getZwbh, jh);
        ALLQj one = allQjService.getOne(allQjLambdaQueryWrapper);
        if (one == null) {
            return Result.fail(400, "井号不存在");
        }

        LambdaQueryWrapper<TimeTable> timeTableLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (timetableVo.getType() == 0) {
            List<Integer> tnumbers = timetableVo.getTnumbers();
            for (int i = 0; i < tnumbers.size(); i++) {
                timeTableLambdaQueryWrapper.and(t -> t.eq(TimeTable::getJh, one.getZwbh()).or().eq(TimeTable::getJh, one.getYwbh()));
//                timeTableLambdaQueryWrapper.eq(TimeTable::getJh, jh);
                timeTableLambdaQueryWrapper.eq(TimeTable::getTnumber, tnumbers.get(i));
                String date = timetableVo.getTtimes().get(i);
                date="2020-01-11 "+date+":10";
                timeTableService.saveOrUpdate(new TimeTable()
                        .setTnumber(tnumbers.get(i))
                        .setTtime(DateUtils.stringToDate(date))
                        .setType(0)
                        .setJh(jh), timeTableLambdaQueryWrapper
                );
                timeTableLambdaQueryWrapper.clear();
            }
        } else if (timetableVo.getType() == 1) {
            timeTableLambdaQueryWrapper.eq(TimeTable::getType, 1);
            timeTableLambdaQueryWrapper.and(t -> t.eq(TimeTable::getJh, one.getZwbh()).or().eq(TimeTable::getJh, one.getYwbh()));
//
            timeTableService.saveOrUpdate(new TimeTable()
                    .setIntervaltime1(timetableVo.getIntervaltime1())
                    .setType(1)
                    .setJh(jh), timeTableLambdaQueryWrapper
            );
            timeTableLambdaQueryWrapper.clear();
        } else {
            timeTableLambdaQueryWrapper.eq(TimeTable::getType, 2);
            timeTableLambdaQueryWrapper.and(t -> t.eq(TimeTable::getJh, one.getZwbh()).or().eq(TimeTable::getJh, one.getYwbh()));
//

            timeTableService.remove(timeTableLambdaQueryWrapper);
            List<Integer> testtimes = timetableVo.getTesttimes();
            for (int i = 0; i < testtimes.size(); i++) {
                timeTableService.save(new TimeTable()
                        .setIntervaltime2(timetableVo.getIntervaltime2s().get(i))
                        .setTesttimes(testtimes.get(i))
                        .setType(2)
                        .setJh(jh)
                );
            }
        }
        return Result.success("add successfully");
    }

    //
    @GetMapping("test")
    public Result test(String userName, String jh, @RequestParam(required = false, defaultValue = "") String jh2) {
        System.out.println(jh);
        System.out.println(jh2);
        return Result.success("");
    }


//    @GetMapping("test")
//    @JHNeedAnnotation2
//    public Result test(String userName, String[] jhs, @RequestParam(required = false) List<String> jhs2) {
////        System.out.println(jhs);
//        for (String jh : jhs) {
//            System.out.println(jh);
//        }
//        System.out.println(jhs2);
//        return Result.success("");
//    }


}
